<template>
  <div>
    <tabs />
    <div class="survey">
      <!-- 流量趋势 -->
      <div class="survey-box">
        <el-row type="flex" justify="space-between" align="middle" class="survey-hedaer">
          <div class="title">流量趋势</div>
          <daterange :school="hasSchool" @update:date="changeDate" @update:school="changeSchool" />
        </el-row>
        <el-row class="traffic-trend">
          <el-col :span="9">
            <el-row :gutter="24">
              <el-col :span="12">
                <div class="visit-item bg1">
                  <div class="number"><count-roll :number="response.home_pv" /></div>
                  <div class="text">访问量(PV)</div>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="visit-item bg2">
                  <div class="number"><count-roll :number="response.home_uv" /></div>
                  <div class="text">独立访客数(UV)</div>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="visit-item bg3">
                  <div class="number"><count-roll :number="response.course_pv" /></div>
                  <div class="text">课程访问量(PV)</div>
                </div>
              </el-col>
              <el-col :span="12">
                <div class="visit-item bg4">
                  <div class="number"><count-roll :number="response.course_uv" /></div>
                  <div class="text">课程访客数(UV)</div>
                </div>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="14" :offset="1"><ve-line :loading="loading" :data="chartData" :settings="chartSettings" :colors="colors" :extend="extendsConfig" /></el-col>
        </el-row>
        <el-row type="flex" :gutter="70">
          <el-col :span="12">
            <el-row type="flex" justify="space-between" align="middle" class="survey-hedaer"><div class="title">用户注册渠道</div></el-row>
            <register :loading="loading" :rows="response.register || []" />
          </el-col>
          <el-col :span="12">
            <el-row type="flex" justify="space-between" align="middle" class="survey-hedaer"><div class="title">用户下单渠道</div></el-row>
            <place-order :loading="loading" :rows="response.place_order || []" />
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import Tabs from '../../tabs/index.vue';
import Daterange from '@/pages/data/components/daterange.vue';
import CountRoll from '@/pages/data/components/count_roll';
import VeLine from 'v-charts/lib/line.common';
import 'v-charts/lib/style.css';
import echarts from 'echarts';
import Register from './components/register.vue';
import PlaceOrder from './components/place_order.vue';

export default {
  components: {
    Tabs,
    Daterange,
    CountRoll,
    VeLine,
    Register,
    PlaceOrder
  },
  data() {
    return {
      daterange: null,
      mhm_id: 1,
      loading: false,
      response: {},
      chartSettings: {
        labelMap: {
          date: '日期',
          pv: '访问量',
          course_pv: '课程访问量'
        },
        xAxisType: 'category',
        yAxisType: ['KMB'],
        area: true,
        scale: [true, false],
        yAxisName: ['访问量']
      },
      chartData: {
        columns: ['date', 'pv', 'course_pv'],
        rows: []
      },
      colors: [
        // 颜色渐变函数 前四个参数分别表示四个位置依次为左、下、右、上
        new echarts.graphic.LinearGradient(0, 1, 0, 0, [
          {
            offset: 0,
            color: '#D6E8FF'
          },
          {
            offset: 0.6,
            color: '#7DADFF'
          }
        ]),
        new echarts.graphic.LinearGradient(0, 1, 0, 0, [
          {
            offset: 0,
            color: '#FFE0E0'
          },
          {
            offset: 0.6,
            color: '#FF9D9D'
          }
        ])
      ],
      extendsConfig: {
        grid: {
          right: 20
        },
        xAxis(v) {
          v.forEach(i => {
            i.boundaryGap = false;
            i.axisLabel = {
              showMaxLabel: true,
              rotate: 45
            };
          });

          return v;
        },
        yAxis(v) {
          v.forEach(i => {
            i.minInterval = 1;
          });

          return v;
        }
      }
    };
  },
  computed: {
    hasSchool() {
      return !!this.response.is_platform;
    }
  },
  created() {
    this.loadData();
  },
  mounted() {
    this.$store.dispatch('setLoading', 0);
  },
  methods: {
    changeDate: function(daterange) {
      this.daterange = daterange;
      this.loading = true;
      this.loadData();
    },
    changeSchool: function(mhm) {
      this.mhm_id = mhm;
      this.loading = true;
      this.loadData();
    },
    loadData: function() {
      this.$http.get(
        '/admin/data/website/survey',
        {
          daterange: this.daterange,
          mhm_id: this.mhm_id
        },
        response => {
          this.response = response.data;
          this.chartData.rows = this.response.rows;
          this.loading = false;
        },
        errror => {
          this.loading = false;
        },
        false
      );
    }
  }
};
</script>

<style lang="scss" scoped>
.survey {
  background-color: #f8f8f8;
  padding-right: 27px;
}
.survey-box {
  background-color: #fff;
  min-height: 150px;
  margin-bottom: 20px;
  padding: 10px 20px 25px;
  .survey-hedaer {
    margin-bottom: 28px;
    height: 36px;
    padding-right: 20px;
    background-color: #f1f8ff;
    .title {
      color: #303133;
      font-size: 15px;
      padding-left: 6px;
      border-left: 2px solid #5191ff;
    }
  }

  .ve-line-box {
    margin-top: 24px;
    border: 1px solid #e4e7ed;
    padding: 20px 20px 0 20px;
  }

  .traffic-trend {
    .visit-item {
      margin-top: 24px;
      height: 120px;
      border-radius: 4px;
      text-align: center;
      color: #fff;
      padding-top: 20px;
      box-sizing: border-box;
      &.bg1 {
        background: linear-gradient(226deg, rgba(115, 164, 255, 1) 0%, rgba(60, 143, 255, 1) 100%);
      }
      &.bg2 {
        background: linear-gradient(226deg, rgba(255, 172, 118, 1) 0%, rgba(255, 142, 70, 1) 100%);
      }
      &.bg3 {
        background: linear-gradient(226deg, rgba(255, 149, 132, 1) 0%, rgba(255, 113, 105, 1) 100%);
      }
      &.bg4 {
        background: linear-gradient(45deg, rgba(123, 114, 255, 1) 0%, rgba(135, 161, 255, 1) 100%);
      }
      .number {
        font-size: 30px;
        font-weight: bold;
      }
      .text {
        margin-top: 15px;
        font-size: 14px;
      }
    }
  }
}
</style>
